package com.baizhi.shane.sys.controller;

import com.baizhi.shane.common.base.Result;
import com.baizhi.shane.constant.SysMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.util.MimeTypeUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@RestController
@RequestMapping("image")
@Api(tags="图片相关")
@Slf4j
public class ImageController {


    @javax.annotation.Resource
    private  ResourceLoader resourceLoader;
    @javax.annotation.Resource
    private ServletContext servletContext;
    @javax.annotation.Resource
    private HttpServletRequest request;
//    public ImageController(ResourceLoader resourceLoader) {
//        this.resourceLoader = resourceLoader;
//    }

    @GetMapping("/logo")
    public ResponseEntity<Resource> getLogoImage() {
        Resource resource = resourceLoader.getResource(SysMapping.CLASS_PATH + "image/ShaneLogo.png");
        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_PNG)
                .body(resource);
    }
    @GetMapping("/getOne")
    @ApiOperation(value="展示图片", notes="展示图片")
    public ResponseEntity<Resource> getImageFile(@RequestParam String filename) throws IOException {
        Resource resource = resourceLoader.getResource("classpath:image/" + filename);
        if (resource.exists()) {
            String mediaType = servletContext.getMimeType(filename);
            return ResponseEntity.ok()
                    .contentType(MediaType.parseMediaType(mediaType))
                    .body(resource);
        } else {
            // 返回文件不存在的错误响应
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("/uploadImage")
    @ApiOperation(value="上传图片", notes="上传图片")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.fail(500,SysMapping.IMAGE_CANT_NULL);
        }

        try {
            String fileName = StringUtils.cleanPath(file.getOriginalFilename());
            Path uploadPath = Paths.get(resourceLoader.getResource("classpath:image/").getFile().getPath());
            String fileExtension = StringUtils.getFilenameExtension(fileName);
            String newFileName = UUID.randomUUID().toString() + "." + fileExtension;
            Path filePath = uploadPath.resolve(newFileName);
            log.info(filePath.toString());
            Files.copy(file.getInputStream(), filePath);

            // 获取基础URL部分
            UriComponentsBuilder uriBuilder = ServletUriComponentsBuilder.fromServletMapping(request).replacePath(null);
            String baseUrl = uriBuilder.build().toString();
            log.info(baseUrl);
            String imageUrl = baseUrl + "/image/getOne?filename=" + newFileName;

//            return ResponseEntity.ok(imageUrl);
            return Result.OK(imageUrl);
        } catch (IOException e) {
            e.printStackTrace();
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败");
            return Result.fail(500,SysMapping.UPLOAD_IMAGE_FAIL);
        }
    }
}